package com.zhs.smartmall.order.controller;



import com.zhs.smartmall.order.service.IMqMessageService;
import com.zhs.smartmall.order.model.MqMessage;
import com.zhs.smartmall.order.dto.MqMessageDto;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhs.smartmallcommon.Enum.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zhs.smartmallcommon.exception.ServiceException;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author zhenghuisheng
 * @since 2023-11-12
 */
@RestController
@RequestMapping("/mq/message")
@Api(value = "")
@Slf4j
public class MqMessageController {

    @Resource
    private IMqMessageService  iMqMessageService;

    @ApiOperation(value = "分页查询")
    @GetMapping("/getByPage")
    public R<IPage<MqMessage>> getListByPage(
        @RequestParam(value = "currentPage", required = false, defaultValue = "1")Integer currentPage,
        @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize){
        //构建分页
        Page<MqMessage> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<MqMessage> lambda = new QueryWrapper<MqMessage>().lambda();
        //此处可以拼条件
        lambda.eq(MqMessage::getIsDeleted,0);
        IPage<MqMessage> pages =  iMqMessageService.page(page, lambda);
        return R.ok(pages);
    }


    @ApiOperation(value = "通过id查询")
    @GetMapping("/getById")
    public R<MqMessage> getById(@RequestParam("id") String id){
        if (StringUtils.isBlank(id)) throw new ServiceException("id不能为空");
        LambdaQueryWrapper<MqMessage> wrapper = new QueryWrapper<MqMessage>()
                            .lambda().eq(MqMessage::getId,id).eq(MqMessage::getIsDeleted,0);
        return R.ok(iMqMessageService.getOne(wrapper));
    }

    @ApiOperation(value = "通过id删除数据")
    @GetMapping("/delete/{id}")
    @Transactional
    public R deleteById(@PathVariable("id") String id){
        if (StringUtils.isBlank(id)) throw new ServiceException("id不能为空");
        //逻辑删除
        LambdaQueryWrapper<MqMessage> query = new QueryWrapper<MqMessage>().lambda().eq(MqMessage::getId, id).eq(MqMessage::getIsDeleted, 0);
        MqMessage mqMessage = iMqMessageService.getOne(query);
        if(ObjectUtils.isEmpty(mqMessage)) throw new ServiceException("该数据不存在或者已经被删除");
        mqMessage.setIsDeleted(1);
        iMqMessageService.updateById(mqMessage);
        return R.ok("数据删除成功");
    }


    @ApiOperation(value = "批量删除数据")
    @PostMapping("/deleteByIds")
    @Transactional
    public R deleteByIds(@RequestBody MqMessageDto mqMessageDto){
        if (CollectionUtils.isEmpty(mqMessageDto.getIdList())) throw new ServiceException("要删除的id不能为空!");
        //逻辑删除
        List<MqMessage> list = new ArrayList<>();
        mqMessageDto.getIdList().stream().forEach(id ->{
            MqMessage mqMessage = iMqMessageService.getById(id);
            if (ObjectUtils.isEmpty(mqMessage)) throw new ServiceException("id为" + id + "的数据不存在");
            if (1 == mqMessage.getIsDeleted()) throw new ServiceException("id为" + id + "的数据已经被删除");
            mqMessage.setIsDeleted(1);
            list.add(mqMessage);
        });
        iMqMessageService.updateBatchById(list);
        return R.ok("数据删除成功");
    }

    @ApiOperation(value = "新增或者更新数据")
    @PostMapping("/addOrUpdate")
    public R addOrUpdate(@RequestBody MqMessageDto mqMessageDto){
        return iMqMessageService.addOrUpdate(mqMessageDto);
    }

}
